package com.ruoyi.syf.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.common.enums.ConvertType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.syf.domain.RiskAnalysisObject;
import com.ruoyi.syf.mapper.RiskAnalysisObjectMapper;
import com.ruoyi.syf.service.IRiskAnalysisObjectService;
import com.ruoyi.syf.service.IRiskAnalysisUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 安全分析对象Service业务层处理
 *
 * @author yr
 * @date 2024-07-04
 */
@Service
public class RiskAnalysisObjectServiceImpl implements IRiskAnalysisObjectService {
    @Autowired
    private RiskAnalysisObjectMapper riskAnalysisObjectMapper;
    @Autowired
    private IRiskAnalysisUnitService unitService;

    /**
     * 查询安全分析对象
     *
     * @param id 安全分析对象主键
     * @return 安全分析对象
     */
    @Override
    public RiskAnalysisObject selectRiskAnalysisObjectById(Long id) {
        return riskAnalysisObjectMapper.selectById(id);
    }

    /**
     * 查询安全分析对象列表
     *
     * @param riskAnalysisObject 安全分析对象
     * @return 安全分析对象
     */
    @ConvertField({
            @ConvertFieldItem(fieldName = "deptId",assistFieldName = "deptName",type = ConvertType.DEPT),
            @ConvertFieldItem(fieldName = "userId",assistFieldName = "userName",type = ConvertType.USER)
    })
    @Override
    public List<RiskAnalysisObject> list(RiskAnalysisObject riskAnalysisObject) {
        return riskAnalysisObjectMapper.list(riskAnalysisObject);
    }

    @Override
    public RiskAnalysisObject selectListByCode(String code) {
        LambdaQueryWrapper<RiskAnalysisObject> lqw = new LambdaQueryWrapper<RiskAnalysisObject>()
                .eq(RiskAnalysisObject::getCode, code)
                .eq(RiskAnalysisObject::getDelFlag, "0");
        return riskAnalysisObjectMapper.selectOne(lqw);
    }

    /**
     * 新增安全分析对象
     *
     * @param riskAnalysisObject 安全分析对象
     * @return 结果
     */
    @Override
    public int insertRiskAnalysisObject(RiskAnalysisObject riskAnalysisObject) {
        if (selectListByCode(riskAnalysisObject.getCode()) != null){
            throw new ServiceException("编码已存在");
        }
        return riskAnalysisObjectMapper.insert(riskAnalysisObject);
    }

    /**
     * 修改安全分析对象
     *
     * @param riskAnalysisObject 安全分析对象
     * @return 结果
     */
    @Override
    public int updateRiskAnalysisObject(RiskAnalysisObject riskAnalysisObject) {
        RiskAnalysisObject object = selectListByCode(riskAnalysisObject.getCode());
        if (object != null && !object.getId().equals(riskAnalysisObject.getId())){
            throw new ServiceException("编码已存在");
        }
        return riskAnalysisObjectMapper.updateById(riskAnalysisObject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upStatus(Long id, String reason) {
        RiskAnalysisObject object = riskAnalysisObjectMapper.selectById(id);

        //校验权限
        SecurityUtils.hasUser(object.getUserId());

        Boolean status = object.getStatus();
        object.setStatus(!status);
        riskAnalysisObjectMapper.updateById(object);
        //批量开启/关闭单元
        return unitService.upStatusList(id,status,reason);
    }

    /**
     * 批量删除安全分析对象
     *
     * @param ids 需要删除的安全分析对象主键
     * @return 结果
     */
    @Override
    public int deleteRiskAnalysisObjectByIds(Long[] ids) {
        return riskAnalysisObjectMapper.deleteRiskAnalysisObjectByIds(ids);
    }

    /**
     * 删除安全分析对象信息
     *
     * @param id 安全分析对象主键
     * @return 结果
     */
    @Override
    public int deleteRiskAnalysisObjectById(Long id) {
        return riskAnalysisObjectMapper.deleteRiskAnalysisObjectById(id);
    }
}
